package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.mapper.CityMapper;
import com.mz.mattress.admin.mapper.RegionMapper;
import com.mz.mattress.admin.service.CityService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.City;
import com.mz.mattress.api.entity.Region;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

@Service
@Slf4j
public class CityServiceImpl implements CityService {

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private RegionMapper regionMapper;

    public static final Map<String, List<String>> MAP = new HashMap<>(64);

    @PostConstruct
    public void init() {

        List<City> list = cityMapper.list();

        for (City city : list) {
            String province = city.getProvince();
            List<String> citys = MAP.get(province);
            if (citys == null) {
                List<String> array = new ArrayList<>();
                array.add(city.getCity());
                MAP.put(province, array);
            } else {
                citys.add(city.getCity());
            }
        }

    }

    @Override
    public Result<Map<String, List<String>>> listAll() {
        return Result.ok(MAP);
    }

    @Override
    public Result<IPage<City>> listPage(String province, String city, Integer status, Integer pageNo, Integer pageSize) {
        Page page = new Page(pageNo, pageSize);
        Page<City> list = cityMapper.listPage(page, province, city, status);
        return Result.ok(list);
    }

    @Override
    public Result<City> getById(Integer id) {
        City city = cityMapper.selectById(id);
        return Result.ok(city);
    }

    @Override
    public Result add(City city) {
        if (StringUtils.isEmpty(city.getProvince()) ||
                StringUtils.isEmpty(city.getCity())) {
            return Result.fail("请输入省和市");
        }
        cityMapper.insert(city);
        return Result.ok();
    }

    @Override
    public Result updateById(City city) {
        cityMapper.updateById(city);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        cityMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    @Override
    public void genData() {

//        List<Region> list = regionMapper.selectByPid(102);
//
//        for (Region region : list) {
//
//            City city = new City();
//            city.setProvince("浙江省");
//            city.setCity(region.getName());
//            city.setStatus(1);
//            cityMapper.insert(city);
//
//        }

        List<Region> list = regionMapper.selectByPid(0);

        for (Region region : list) {

            if (region.getId() == 102) {
                continue;
            }

            List<Region> cityList = regionMapper.selectByPid(region.getId());
            for (Region cityRegion : cityList) {

                City city = new City();
                city.setProvince(region.getName());
                city.setCity(cityRegion.getName());
                city.setStatus(1);
                cityMapper.insert(city);

            }


//            City city = new City();
//            city.setProvince("浙江省");
//            city.setCity(region.getName());
//            city.setStatus(1);
//            cityMapper.insert(city);

        }


    }

}
